//
//  NSObject+HDUserInformationHttpRequestManger.m
//  app
//
//  Created by D on 15/4/13.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDUserInforHttpRequestManger.h"
#import "JKHttpClient.h"
#import "HDLoginUserURL.h"
#import "HDHeaderModel.h"
#import "SNSessionManager.h"

static HDUserInforHttpRequestManger * _userInfoHttpRequestManger = nil;
@interface HDUserInforHttpRequestManger ()

@property (strong,nonatomic) JKHttpClient * jkHttp;
@property (nonatomic, retain) SNUniteUrl * uniteUrl;
@property (strong,nonatomic) JKHttpClient * jkImageHttp;

@end


@implementation HDUserInforHttpRequestManger

+(instancetype)shareUserInforHttpRequestManger
{
    if (_userInfoHttpRequestManger == nil) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            _userInfoHttpRequestManger = [[self alloc] initManager];
        });
    }
    return _userInfoHttpRequestManger;
}

-(instancetype)initManager
{
    if (self = [super init]) {
//        self.jkHttp = [[JKHttpClient alloc] initWithUrlBase:URL_BASE_USER saveCookiesTo:kUserDefaultHttpCookies];
        self.uniteUrl = [SNUniteUrl shareUniteUrl];
        self.jkHttp = [[JKHttpClient alloc] init];
        self.jkImageHttp = [[JKHttpClient alloc]init];
    }
    _userInfoHttpRequestManger = self;
    return _userInfoHttpRequestManger;
}

//退出登录
-(void)loginOutWithParams:(NSDictionary*)params result:(JKRemoteResult*)remote
{
    if (self.jkHttp.isRunning) {
        return;
    }
    
    [self.jkHttp get:self.uniteUrl.per_loginOutUrl params:params result:^(JKResponse *resp) {
        NSLog(@"%@",resp.content);
        remote.info = resp.content;
        
        if (resp.success) {
            /** 每次退出登录之后清楚 cookies*/
            [SNSessionManager clearCookies];
        }
        
        [remote call:resp];
    }];
}
//更换头像
-(void)changeFaceWithParams:(NSDictionary*)params result:(JKRemoteResult*)remote data:(NSData*)data
{
    if (self.jkHttp.isRunning) {
        return;
    }
    NSLog(@"%@",params);
//    NSString * url = @"http://test.kangxihui.com/uploads/face";
     NSString * url = @"http://test.kangxihui.com/test";
    [self.jkImageHttp upload:self.uniteUrl.perm_uploadFaceImgUrl data:data params:params result:^(JKResponse *resp) {
        remote.info = nil;
        [remote call:resp];
    }];
    
}

//版本检测
-(void)checkVersionWithParams:(NSDictionary*)params result:(JKRemoteResult*)remote
{
    if (self.jkHttp.isRunning) {
        return;
    }
    [self.jkHttp get:self.uniteUrl.gen_versionUrl params:params result:^(JKResponse *resp) {
        remote.info = resp.content;
        [remote call:resp];
    }];
}
//更改昵称
-(void)changeUserInfoWithParams:(NSDictionary*)params reuslt:(JKRemoteResult*)remote
{
    if (self.jkHttp.isRunning) {
        return;
    }
    [self.jkHttp post:self.uniteUrl.per_uploadUserNickNameUrl params:params result:^(JKResponse *resp) {
        remote.info = resp.content;
        [remote call:resp];
    }];
}

//提交手机号码
-(void)submitPhoneNumberWihtParams:(NSDictionary*)params result:(JKRemoteResult*)remote
{
    if (self.jkHttp.isRunning) {
        return;
    }
    [self.jkHttp post:self.uniteUrl.per_submitPhotoNumberUrl params:params result:^(JKResponse *resp) {
        remote.info = resp.content;
        [remote call:resp];
    }];
}
//注册时登陆
-(void)resgiserEndWithParams:(NSDictionary*)params result:(JKRemoteResult*)remote data:(NSData*)data
{
    if (self.jkHttp.isRunning) {
        return;
    }
    [self.jkHttp upload:self.uniteUrl.per_phoneRegsiterUrl data:data params:params result:^(JKResponse *resp) {
        
        remote.info = resp.content;
        [remote call:resp];
    }];
}
//获取个人数据
-(void)getUserInfoWithParams:(NSDictionary*)params result:(JKRemoteResult*)remote
{
    if (self.jkHttp.isRunning) {
        return;
    }
    [self.jkHttp get:self.uniteUrl.per_getUserInfoUrl params:params result:^(JKResponse *resp) {
        
        DLog(@"%@",resp.content);
        
        remote.info = resp.content[@"user_info"];
        NSLog(@"content = %@",resp.content[@"user_info"]);
        [remote call:resp];
    }];
}
//第三方登陆
-(void)thirdLoginWithParams:(NSDictionary*)params result:(JKRemoteResult*)remote
{
    if (self.jkHttp.isRunning) {
        return;
    }
    [self.jkHttp post:self.uniteUrl.per_loginUrl params:params result:^(JKResponse *resp) {
        

        remote.info = resp.content;
        [remote call:resp];
    }];
}
//本地登陆
-(void)localLoginWithParams:(NSDictionary*)params result:(JKRemoteResult*)remote
{
    if (self.jkHttp.isRunning) {
        return;
    }
    [self.jkHttp post:self.uniteUrl.per_localLoginUrl params:params result:^(JKResponse *resp) {
        
        remote.info = resp.content;
        [remote call:resp];
    }];
}

//获取分享信息
-(void)getShareMessageWithParams:(NSString*)discuz_id topic_id:(NSString*)topic_id result:(JKRemoteResult*)remote
{
    if (self.jkHttp.isRunning) {
        return;
    }
    
    NSString * url_share = [NSString stringWithFormat:@"%@?discuz_id=%@&tie_id=%@",self.uniteUrl.per_getShareInfoUrl,discuz_id,topic_id];
    
   [self.jkHttp get:url_share result:^(JKResponse *resp) {
       remote.info = resp.content;
       [remote call:resp];
   }];
}


//提交反馈
-(void)submitOpinionWithParams:(NSDictionary*)params result:(JKRemoteResult*)remote
{
    if (self.jkHttp.isRunning) {
        return;
    }
    
    [self.jkHttp post:self.uniteUrl.log_feedBackByUserUrl params:params result:^(JKResponse *resp) {

        remote.info = resp.content;
    
        [remote call:resp];
        
    }];
    
}

//签到
-(void)signInWithResult:(JKRemoteResult*)remote
{
    if (self.jkHttp.isRunning) {
        return;
    }
    
    [self.jkHttp get:self.uniteUrl.per_checkinUrl params:nil result:^(JKResponse *resp) {
        DLog(@"message = %@",resp.message);
        remote.info = resp.content;
        [remote call:resp];
        
    }];
    

}

@end








